<template>
  <div class="categories">
    <div class="categories_header">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <el-card shadow="hover" class="categories_con">
      <el-button type="primary" @click="Addcategories">添加分类</el-button>

      <!-- 表格 -->
      <tree-table v-loading="loading" class="treeTable" :data="categoriesList" :columns="columns"
        :stripe='true' :tree-type='true' :show-index='true' index-text='#' :selection-type="false"
        :expand-type='false'>
        // eslint-disable-next-line vue/no-unused-vars
        <template slot="delete" scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted == false"
            style="color:lightgreen"></i>
          <i class="el-icon-error" v-if="scope.row.cat_deleted == true" style="color:red"></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == '0'">
            一级</el-tag>
          <el-tag type="success" v-else-if="scope.row.cat_level == '1'">
            二级</el-tag>
          <el-tag type="warning" v-else>
            三级</el-tag>
        </template>
        // eslint-disable-next-line
        <template slot="operation" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </tree-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="queryinfo.pagenum" :page-size="queryinfo.pagesize"
          layout="total, prev, pager, next" :total="total">
        </el-pagination>
      </div>

      <!-- 添加框 -->
      <el-dialog center title="添加分类" :visible.sync="dialogAddgories" :before-close="handleClose"
        @close="addcancel">
        <el-form :model="AddGories" label-width="100px" ref="reference">
          <el-form-item label="分类名称">
            <el-input v-model="AddGories.cat_name" clearable></el-input>
          </el-form-item>
          <el-form-item label="父级分类">
            <el-cascader v-model="AddChooseResult" :options="AddChoose" :props="cateProps"
              @change="handleChooseChange" clearable>
            </el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddgories = false">取 消</el-button>
          <el-button type="primary" @click="confirmAdd">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 编辑框 -->
      <el-dialog title="编辑分类名" :visible.sync="dialogEditorVisible" :before-close="handleClose"
        @close="cancelEditor">
        <el-form>
          <el-form-item label="分类名称" label-width="100px">
            <el-input v-model="formEditor" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditorVisible = false">取 消</el-button>
          <el-button type="primary" @click="EditorConfirm">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'CateGories',
  data() {
    return {
      // 表格加载
      loading: true,
      // 添加框的显示
      dialogAddgories: false,
      // 商品分类列表的数据
      categoriesList: [],
      queryinfo: {
        type: 3,
        pagesize: 5,
        pagenum: 1,
      },
      total: 0,
      // 表格的列设置
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
          width: '400px',
        },
        {
          label: '是否有效',
          Width: '200px',
          type: 'template',
          template: 'delete',
        },
        {
          label: '排序',
          Width: '200px',
          type: 'template',
          template: 'level',
        },
        {
          label: '操作',
          Width: '400px',
          type: 'template',
          template: 'operation',
        },
      ],
      // 添加框
      AddGories: {
        cat_pid: 0,
        cat_name: '',
        cat_level: 0,
      },
      // 添加框的级联选择器
      AddChoose: [],
      // 添加框级联选择器选择结果是数组
      AddChooseResult: [],
      // 添加框级联选择格式设置
      cateProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: 'true',
      },
      // 编辑框的显示
      dialogEditorVisible: false,
      // 编辑框需要的id
      EditorId: '',
      // 编辑框
      formEditor: '',
    }
  },
  methods: {
    async getCateGories() {
      const { data: CateGoriesres } = await this.$http.get('categories', { params: this.queryinfo })
      // console.log(CateGoriesres)
      this.total = CateGoriesres.data.total
      this.categoriesList = CateGoriesres.data.result
      this.loading = false
    },
    // 分页每页数据条数 改变时会触发
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.getCateGories()
    },
    // 当前页数 改变时会触发
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.getCateGories()
    },
    // 打开添加框
    async Addcategories() {
      this.dialogAddgories = true
      const { data: AddChooseres } = await this.$http.get('categories', { params: { type: 2 } })
      // console.log(AddChooseres)
      this.AddChoose = AddChooseres.data
    },
    // 确认关闭添加框、修改框
    handleClose(done) {
      this.$confirm('确认关闭?填写数据将不被保存')
        .then(() => {
          done()
        })
        .catch(() => {})
    },
    // 关闭添加框
    addcancel() {
      this.AddGories.cat_name = ''
      this.AddChooseResult = []
    },
    // 级联选择器选择后
    handleChooseChange() {
      if (this.AddChooseResult.length == 0) {
        this.AddGories.cat_pid = 0
        this.AddGories.cat_level = 0
      }
      if (this.AddChooseResult.length == 1) {
        this.AddGories.cat_pid = this.AddChooseResult[0]
        this.AddGories.cat_level = 1
      }
      if (this.AddChooseResult.length == 2) {
        this.AddGories.cat_pid = this.AddChooseResult[1]
        this.AddGories.cat_level = 2
      }
    },
    // 添加框点击确认
    async confirmAdd() {
      const { data: confirmAddres } = await this.$http.post('categories', this.AddGories)
      if (confirmAddres.meta.status != 201) {
        this.$message.error('创建失败')
        return
      }
      this.$message.success('创建成功')
      this.getCateGories()
      this.dialogAddgories = false
    },
    // 点击编辑按钮
    handleEdit(a, b) {
      this.EditorId = b.cat_id
      this.dialogEditorVisible = true
    },
    // 关闭编辑框
    cancelEditor() {
      this.formEditor = ''
    },
    // 编辑框点击确认
    async EditorConfirm() {
      const { data: EditorConfirmres } = await this.$http.put(`categories/${this.EditorId}`, {
        cat_name: this.formEditor,
      })
      if (EditorConfirmres.meta.status != 200) {
        this.$message.error('创建失败')
        return
      }
      this.$message.success('创建成功')
      this.getCateGories()
      this.dialogEditorVisible = false
    },
    // 点击删除按钮
    handleDelete(a, b) {
      this.$confirm('此操作将永久删除此分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const { data: deleteres } = await this.$http.delete(`categories/${b.cat_id}`)
          if (deleteres.meta.status != 200) {
            this.$message.error('删除失败，请重试')
          }
          this.$message.success('删除成功')
          this.getCateGories()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
  },
  created() {
    this.getCateGories()
  },
}
</script>

<style lang='less' scoped>
.categories {
  height: 100%;
}
.categories_header {
  .el-breadcrumb-item {
    font-size: 17px;
  }
}
.categories_con {
  margin-top: 30px;
  background: #fff;
  padding: 25px 20px;
  .el-table {
    margin-top: 20px;
  }
}
.treeTable {
  margin: 18px 0;
}
</style>